/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import java.util.List;

import com.google.api.client.json.GenericJson;

import android.content.Context;
import android.util.Log;

public class EndpointsListLoader<T extends GenericJson> extends AbstractEndpointsLoader<List<T>> {
		
	public EndpointsListLoader(Context context, Class<T> objClass, Object... filters) {
		super(context, objClass, filters);
		method = "list" + objClass.getSimpleName();
	}
		 
	@Override
	public List<T> loadInBackground() {
		try {
			GenericJson response = load();
			//TODO: manage remote error
			//if (response.containsKey("error")...
			@SuppressWarnings("unchecked")
			List<T> ret = (List<T>) response.getClass()
					.getMethod("getItems", new Class[] {})
					.invoke(response, new Object[] {});
			return ret;
		} catch(Exception e) {
			Log.e(SolidalApp.TAG, "Error loading data", e);
		}
		return null;
	}
	
}
